package cn.itcast.goods.user.dao;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import cn.itcast.goods.user.domain.User;
import cn.mytools.jdbc.TxQueryRunner;

/**
 * 用户模块持久层
 * @author HL
 *
 */
public class UserDao {
	private QueryRunner qr = new TxQueryRunner(); 
	/**
	 * 校验用户名是否注册
	 * @param loginname
	 * @return
	 * @throws SQLException
	 */
	public boolean ajaxValidateLoginname(String loginname) throws SQLException {
		String sql = "select count(*) from t_user where loginname=?";
		Number number = (Number) qr.query(sql, new ScalarHandler(),loginname);
		return number.intValue() == 0;
	}
	/**
	 * 校验email是否注册
	 * @param email
	 * @return
	 * @throws SQLException
	 */
	public boolean ajaxValidateEmail(String email) throws SQLException {
		String sql = "select count(*) from t_user where email=?";
		Number number = (Number) qr.query(sql, new ScalarHandler(),email);
		return number.intValue() == 0;
	}
	/**
	 * 添加用户
	 * @param user
	 * @throws SQLException
	 */
	public void add(User user) throws SQLException {
		String sql = "insert into t_user values(?,?,?,?,?,?)";
		Object[] params = {user.getUid(),user.getLoginname(),user.getLoginpass(),
				user.getEmail(),user.getStatus(),user.getActivationCode()};
		qr.update(sql,params);
	}
	
	/**
	 * 通过激活码查询用户 
	 * @param activationCode
	 * @return
	 * @throws SQLException 
	 */
	public User findByActivationCode(String activationCode) throws SQLException {
		String sql = " select * from t_user where activationCode=? ";
		return qr.query(sql, new BeanHandler<User>(User.class),activationCode);
	}
	/**
	 * 修改激活状态
	 * @param uid
	 * @param status
	 * @throws SQLException 
	 */
	public void updateStatus(String uid, int status) throws SQLException {
		String sql = " update t_user set status=? where uid=? ";
		qr.update(sql,status,uid);
	}
	
	/**
	 * 通过用户名和密码查询用户
	 * @param username
	 * @param password
	 * @return user
	 * @throws SQLException 
	 */
	public User findByUserNameAndPassWord(String loginname, String loginpass) throws SQLException {
		String sql = "select * from t_user where loginname=? and loginpass=?";
		return qr.query(sql, new BeanHandler<User> (User.class),loginname,loginpass);
	}
	/**
	 * 修改密码
	 * @param loginname
	 * @param newpass
	 * @throws SQLException 
	 */
	public void updatePassword(String loginname, String newpass) throws SQLException {
		String sql = "update t_user set loginpass=? where loginname=?";
		qr.update(sql,newpass,loginname);
		
	}
	public User findByUid(String uid) throws SQLException {
		String sql = "select * from t_user where uid=?";
		return qr.query(sql, new BeanHandler<User> (User.class),uid);
	}
	public boolean findByLoginnameAndEmail(String loginname, String email) throws SQLException {
		String sql = "select count(*) from t_user where loginname=? and email = ?";
		Number number = (Number) qr.query(sql, new ScalarHandler(),loginname,email);
		return number.intValue() != 0 ;
	}
}
